import {refreshCaptcha} from "./utils.js";

const login = $(".login")
const captcha_img = $("#captcha")

captcha_img.click(function () {
    refreshCaptcha(captcha_img)
})

login.click(function () {
    const username = $("#username").val()
    const password = $("#password").val()
    const captcha = $("#captcha_code").val()

    $.ajax({
        url: "http://www.woniu-news.com:8082/login.php", type: "POST", xhrFields: {
            withCredentials: true
        },
        data: {
            username,
            password,
            captcha,
            credentials: 'include'
        },
        dataType: "json",
        success: (resp) => {
            if (resp.code === 200) {
                alert(resp.message)
                window.location.assign("index.html")
            } else {
                alert(resp.message)
            }
            refreshCaptcha(captcha_img)
        },
        error: (resp) => {
            alert(resp.message)
            refreshCaptcha(captcha_img)
        }
    })
})

// 监听回车键
$(document).ready(function () {
    $('form').on('keypress', function (event) {
        if (event.which === 13) { // 回车键
            login.click() // 触发登录按钮点击事件
        }
    })
})
